﻿function submitBidSellAuction(auctionId, curUserAccName) {
    var lowestPrice = $("#lowestPrice" + auctionId).val();
    var postedDate = new Date();
    var price = $('#bidPrice' + auctionId).val();
    if (price >= lowestPrice) {
        $('#bidPriceError' + auctionId).html("Giá không hợp lệ");
    } else {
        $('#bidPriceError' + auctionId).html("");
        hideBidPopup(auctionId);
        var data2Send = { "auctionId": auctionId, "curUserAccName": curUserAccName, "postedDate": postedDate.toUTCString(), "price": price };
        $.ajax({
            type: "POST",
            url: '/Auction/BidSellAuction',
            data: JSON.stringify(data2Send),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) { //call successfull
                if (data.isOK == -1) {
                    $("#ErrorPanelDiv" + auctionId).html("Thất bại");
                }else  if (data.isOK == -2) {
                    $("#ErrorPanelDiv" + auctionId).html("Đã hết thời gian đấu giá");
                }else {
                    $("#ErrorPanelDiv" + auctionId).html("Thành công");
                }
            },
            error: function (xhr) {
                //error occurred
            }
        });
    }
}

function setAutoLoadNewAuctioneers(auctionId) {
    setInterval(function () {  loadNextAuctioneers(auctionId); }, 5000);
}
function showBidPopUp(auctionId) {
    var data2Send = { "auctionId": auctionId };
    $.ajax({
        type: "POST",
        url: '/Auction/GetLowestPrice',
        data: JSON.stringify(data2Send),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) { //call successfull
            $("#bidPrice" + auctionId).val(data.lowestPrice);
            $("#lowestPrice" + auctionId).val(data.lowestPrice);
            $("#lowestPriceDiv" + auctionId).html("Giá thấp nhất hiện tại: " + data.lowestPrice);
            $('#dim').show();
            $("#bidPopup" + auctionId).show();
        },
        error: function (xhr) {
            //error occurred
            $('#dim').show();
            $("#bidPopup" + auctionId).show();
        }
    });   
}
function hideBidPopup(auctionId) {
    $('#dim').hide();
    $("#bidPopup" + auctionId).hide();
}
function loadNextAuctioneers(auctionId) {
    var startedId = $("#nextLastAuctioneerId" + auctionId).val();
    var data2Send = '{"auctionId":' + auctionId.toString() + ',"startedId":' + startedId.toString() + '}';
    $.ajax({
        type: "POST",
        url: '/Auction/LoadNextSellAuctionAuctioneers',
        data: data2Send,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) { //call successfull
            if (data.nextLastId > startedId) {
                $("#auctioneersPanel" + auctionId).html(data.content + $("#auctioneersPanel" + auctionId).html());
                //$(data.content).insertBefore("#auctioneersPanel" + auctionId);
                if (data.previousLastId > 0) {
                    $("#nextLastAuctioneerId" + auctionId).val(data.nextLastId);
                } else {
                    //$("#previousAuctioneersButton" + auctionId).remove();
                    $("#nextLastAuctioneerId" + auctionId).val(data.nextLastId);
                }
            }
        },
        error: function (xhr) {
            //error occurred
        }
    });
}

function loadPreviousAuctioneers(auctionId) {
    var startedId = $("#previousLastAuctioneerId"+auctionId).val();
    var data2Send = '{"auctionId":' + auctionId.toString() + ',"startedId":' + startedId.toString() + '}';
    $.ajax({
        type: "POST",
        url: '/Auction/LoadPreviousSellAuctionAuctioneers',
        data: data2Send,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) { //call successfull
            $("#auctioneersPanel"+auctionId).append(data.content);
            if (data.previousLastId > 0) {
                $("#previousLastAuctioneerId" + auctionId).val(data.previousLastId);
            } else {
                $("#previousAuctioneersButton"+auctionId).remove();
                $("#previousLastAuctioneerId" + auctionId).remove();
            }
        },
        error: function (xhr) {
            //error occurred
        }
    });
}